package com.glodon.ggzy.fwpt.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.Constants;
import com.glodon.ggzy.core.utils.JsonUtils;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.core.utils.QuerySupport;
import com.glodon.ggzy.core.utils.Tools;
import com.glodon.ggzy.entity.GgCqXmGgzy;
import com.glodon.ggzy.entity.GgJsProjectInfo;
import com.glodon.ggzy.entity.GgJsTenderNotice;
import com.glodon.ggzy.entity.GgTdGg;
import com.glodon.ggzy.entity.GgTdProjItem;
import com.glodon.ggzy.entity.GgZfBulletin;
import com.glodon.ggzy.fwpt.service.ICqjyProjectService;
import com.glodon.ggzy.fwpt.service.IJsgcProjectService;
import com.glodon.ggzy.fwpt.service.ITdfcProjectService;
import com.glodon.ggzy.fwpt.service.IZfcgProjectService;
import com.glodon.ggzy.sso.utils.TimeUtil;
import com.glodon.ggzy.vo.CqjyZbgg;
import com.glodon.ggzy.vo.JsgcInfoVO;
import com.glodon.ggzy.vo.Xxgk;
import com.glodon.ggzy.vo.ZfcgInfoVO;

/**
 * 
 * 招标公告控制处理类
 * @author yangjf
 * @since jdk1.6
 * @date 2013-6-17
 *
 */
@Controller
public class ZbggController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IJsgcProjectService jsgcProjectService;
	
	@Autowired
	ICqjyProjectService cqjyProjectService;

	/**
	 * 招标公告 - 建设工程 分页 
	* @param model
	* @return
	*/
	@RequestMapping("/fwpt/zbgg_jsgc_page_list")
	public String zbggJsgcPageList(Model model) {
		return "/fwpt/zbgg_jsgc_page_list";
	}

	/**
	 * 获取招标公告-建设工程详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_jsgc_detail")
	public String getZbgg_JsgcDetail(String id, Model model) {
		String retUrl = "";
		GgJsProjectInfo projectInfo = jsgcProjectService.getGgJsProjectInfoById(id);
		GgJsTenderNotice tenderNotice = jsgcProjectService.getGgJsTenderNoticeByProjId(id);
		if (projectInfo != null) {
			projectInfo.setProjectType(jsgcProjectService.getProjectTypeByCode(projectInfo.getProjectType()));
			projectInfo.setJointBidding(jsgcProjectService.getJointBiddingByCode(projectInfo.getJointBidding()));
		}
		model.addAttribute("projectInfo", projectInfo);
		model.addAttribute("tenderNotice", tenderNotice);
		model.addAttribute("id", id);
		model.addAttribute("liuchengType", "zbgg");
		if (tenderNotice != null) {
			if (tenderNotice.getNoticeType().equals("zbgg")) {
				retUrl = "/fwpt/zbgg_jsgc_detail";
			} else if (tenderNotice.getNoticeType().equals("fjczbgg")) {
				retUrl = "/fwpt/zbgg_jsgc_fjcdetail";
			} else {
				retUrl = "/fwpt/zbgg_jsgc_detail";
			}
		}
		return retUrl;
	}

	/**
	 * 获取招标公告-建设工程详情:分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_jsgc_page_data")
	public void getZbgg_JsgcPageList(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		Page queryPage = jsgcProjectService.queryJsgcProjectInfo(qm, searchText);
		List<GgJsProjectInfo> noticeList = (List<GgJsProjectInfo>) queryPage.getResult();
		List<JsgcInfoVO> zbggList = new ArrayList<JsgcInfoVO>();
		for (GgJsProjectInfo project : noticeList) {
			JsgcInfoVO zbgg = new JsgcInfoVO(project.getId(), project.getProjectName(), jsgcProjectService.getProjectTypeByCode(project
					.getProjectType()), Tools.formatDateToString(project.getBidEndTime()), project.getId());
			zbggList.add(zbgg);
		}
		queryPage.setResult(zbggList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 招标公告 - 产权交易 分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_cqjy_page_list")
	public String zbggCqjyPageList(Model model) {
		return "/fwpt/zbgg_cqjy_page_list";
	}

	/**
	 * 获取招标公告-产权交易详情:分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_cqjy_page_data")
	public void getZbggCqjyPageList(Model model, int rows, int page, String pkgName, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		qm.addParamToMap(QuerySupport.QUERYCLASS, GgCqXmGgzy.class);
		qm.addParamToMap(QuerySupport.ORDERBYDESC, "gpDate");
		if (StringUtils.isNotEmpty(pkgName)) {
			try {
				qm.addParamToMap("pkgName_like", "%" + URLDecoder.decode(pkgName, "UTF-8") + "%");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		qm.addParamToMap("fb", "1");
		Page queryPage = cqjyProjectService.queryPage(qm);

		List<GgCqXmGgzy> zbggList = (List<GgCqXmGgzy>) queryPage.getResult();
		List<CqjyZbgg> resultList = new ArrayList<CqjyZbgg>();
		CqjyZbgg zbgg = null;
		for (GgCqXmGgzy gg : zbggList) {
			String gpDate = gg.getGpDate() == null ? "" : Tools.formatDateToString(gg.getGpDate());
			zbgg = new CqjyZbgg(gg.getId(), gg.getPrjNo(), gg.getPrjType(), gg.getPkgName(), gpDate, gg.getDjs() == null ? 0 : gg.getDjs());
			resultList.add(zbgg);
		}
		queryPage.setResult(resultList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 招标公告 - 政府采购  分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_zfcg_page_list")
	public String zbggZfcgPageList(Model model) {
		return "/fwpt/zbgg_zfcg_page_list";
	}

	/**
	 * 获取招标公告-政府采购详情:分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_zfcg_page_data")
	public void getZbggZfcgPageList(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		//查查参数赋值
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}

		Page queryPage = zfcgProjectService.queryZfcgTenderNotice(qm, searchText, Constants.FWPT_ZBGG);
		List<GgZfBulletin> noticeList = (List<GgZfBulletin>) queryPage.getResult();
		List<ZfcgInfoVO> xxgkList = new ArrayList<ZfcgInfoVO>();
		for (GgZfBulletin bulletin : noticeList) {
			ZfcgInfoVO xxgk = new ZfcgInfoVO(bulletin.getId(), bulletin.getBulletincode(), bulletin.getBulletinname(), bulletin.getStockmode(),
					bulletin.getBuysarttime());
			xxgkList.add(xxgk);
		}
		queryPage.setResult(xxgkList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 招标公告 - 土地房产 分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_tdfc_page_list")
	public String zbggTdfcPageList(Model model) {
		return "/fwpt/zbgg_tdfc_page_list";
	}

	/**
	 * 获取招标公告-土地房产分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_tdfc_page_data")
	public void getZbgg_TdfcPageList(Model model, String prjName, int rows, int page, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);

		String name = "";
		if (StringUtils.isNotEmpty(prjName)) {
			try {
				name = "%" + URLDecoder.decode(prjName, "UTF-8") + "%";
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				name = "";
			}
		}

		if (StringUtils.isNotEmpty(name)) {
			qm.addParamToMap("ggName_like", name);
		}

		qm.addParamToMap(QuerySupport.QUERYCLASS, GgTdGg.class);
		qm.addParamToMap(QuerySupport.ORDERBYDESC, "ggUploadtime");
		Page queryPage = tdfcProjectService.queryZbggTdfcPage(qm);
		List<GgTdGg> prjList = (List<GgTdGg>) queryPage.getResult();

		List<Xxgk> peList = new ArrayList<Xxgk>();

		for (GgTdGg prj : prjList) {
			Xxgk xx = new Xxgk(prj.getGgName(), null, prj.getId(), null, TimeUtil.getDate(prj.getGgEndtime()));
			peList.add(xx);
		}

		queryPage.setResult(peList);

		String jsonStr = "";

		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Autowired
	IZfcgProjectService zfcgProjectService;

	/**
	 * 政府采购
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_zfcg_list")
	public String getZbgg_ZfcgList(Model model) {
		model.addAttribute("list", null);
		return "/fwpt/zbgg_zfcg_list";
	}

	/**
	 * 获取招标公告-政府采购详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_zfcg_detail")
	public String getZbgg_ZfcgDetail(String id, Model model) {
		GgZfBulletin bulletin = zfcgProjectService.getGgZfBulletinById(id);
		model.addAttribute("bulletin", bulletin);
		return "/fwpt/zbgg_zfcg_detail";
	}

	@Autowired
	ITdfcProjectService tdfcProjectService;



	/**
	 * 获取招标公告-土地房产详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/zbgg_tdfc_detail")
	public String getZbgg_TdfcDetail(String id, Model model) {

		GgTdGg queryGgTdGgById = tdfcProjectService.queryGgTdGgById(id);
		String prjId = queryGgTdGgById.getPrjId();

		//写业务逻辑
		List<GgTdProjItem> list = tdfcProjectService.queryGgTdProItemsById(prjId);
		String remark = "无";

		String gp_zd = "";

		for (int i = 0; i < list.size(); i++) {

			GgTdProjItem ggTdProjItem = list.get(i);

			gp_zd += ggTdProjItem.getGpxhZdbh();

			if (i < list.size() - 1)
				gp_zd = gp_zd + "、";

			if (StringUtils.isEmpty(ggTdProjItem.getBz()))
				continue;

			remark = ggTdProjItem.getBz();

		}

		//获取公告信息
		GgTdGg notice = tdfcProjectService.queryGgTdGgByProId(prjId);

		//标地明细
		model.addAttribute("list", list);
		model.addAttribute("remark", remark);
		//公告
		model.addAttribute("notice", notice);
		//挂牌序号
		model.addAttribute("gp_zd", gp_zd);

		//公告发布日期
		model.addAttribute("publicDate", TimeUtil.DateToCN(notice.getGgUploadtime()));

		return "/fwpt/zbgg_tdfc_detail";
	}



	
}
